package com.hfzy.ihk.web.aliyuncall.biz;

import com.hfzy.ihk.common.util.string.StringTools;
import com.hfzy.ihk.common.util.uuid.base64uuid.UUIDs;
import com.hfzy.ihk.facade.aliyuncall.enums.CallTypeEnum;
import com.hfzy.ihk.facade.aliyuncall.enums.MsgTypeEnum;
import com.hfzy.ihk.web.aliyuncall.constant.AliyunAkInfo;
import com.hfzy.ihk.web.aliyuncall.constant.PublicConfig;
import com.hfzy.ihk.web.aliyuncall.context.DisruptorInitBean;
import com.hfzy.ihk.web.aliyuncall.support.disruptor.DisruptorSupport;
import com.hfzy.ihk.web.aliyuncall.support.disruptor.base.DisruptorEvent;
import com.hfzy.ihk.web.aliyuncall.vo.AliyunCallState;
import com.hfzy.ihk.web.aliyuncall.vo.VoiceCallReportVo;
import com.hfzy.ihk.web.aliyuncall.vo.VoiceRTASRReportVo;
import com.lmax.disruptor.RingBuffer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by YANFA on 2018/7/31.
 */
//@Lazy
@Component("aliyunCallStateBiz")
public class AliyunCallStateBiz extends DisruptorSupport<AliyunCallState,DisruptorEvent<AliyunCallState>> {

    private static final Logger logger = LoggerFactory.getLogger(AliyunCallStateBiz.class);
    @Autowired
    PushDataToAnalysis pushDataToAnalysis;

    @Override
    protected RingBuffer<DisruptorEvent<AliyunCallState>> useRingBuffer(AliyunCallState data) {
        int index = 0;
        RingBuffer ringBuffer = null;

        String useType = "阿里云中间状态";

        String sessinId =data.getOut_id();
        if(StringTools.isEmpty(sessinId))
            return null;
        //session 做hash 判断应该进哪一个队列
        index = sessinId.hashCode()% PublicConfig.ALIYUN_CALL_STATE_BUFFER_NUM_SIZE;

        ringBuffer = DisruptorInitBean.getRingBufferAliyunCallState( Math.abs(index));

        checkRingBufferUse(ringBuffer,useType);
        return ringBuffer;
    }

    /**
     * 处理状态消息
     * @param
     */

    public void dealCallState(AliyunCallState aliyunCallState,String msgType){

        if(StringTools.isEmpty(aliyunCallState)){
            return;
        }
        if((!AliyunAkInfo.VOICEREPORT.equals(msgType))||("voice".equals(aliyunCallState.getVoice_type()))) { //语音中间状态和账单
            aliyunCallState.setMsgType(msgType);
            putInRingBuffer(aliyunCallState);
        }else if("asr".equals(aliyunCallState.getVoice_type())){ //asr语音识别账单
            //接受到该话单推送结束状态给分析服务器，告诉他们这个通话已经结束了
            String[] packageIdAndCallTaskId = pushDataToAnalysis.readPackageIdAndCallTaskId(aliyunCallState.getOut_id());
            if(packageIdAndCallTaskId!=null&&packageIdAndCallTaskId.length>1) {
                logger.info("----->发送asr账单到分析服务器packageIdAndCallTaskId：{}",packageIdAndCallTaskId);
                try {
                    pushDataToAnalysis.pushData(packageIdAndCallTaskId[0], packageIdAndCallTaskId[1], aliyunCallState.getOut_id(),  new VoiceRTASRReportVo(), CallTypeEnum.DOUBLE_CALL.getValue(), MsgTypeEnum.END.getValue());
                }catch (Exception e){
                    logger.error("--------->推送识别结果到分析服务器异常，sessionId:{}",aliyunCallState.getOut_id(),e);
                }
            }
        }
    }
}
